#if !SERVER
using ActorSystem;
using Net.Share;
using UnityEngine;

namespace Example2
{
    public partial class Player : Actor, IActorOnCollisionEnter, IActorOnCollisionExit
    {
        public ActorView bullet;
        public Transform firePoint;
        public bool isGround = true;
        internal Rigidbody rigidbody;
        public float jumpSpeed = 500f;

        protected override void EnterArea(in Operation opt)
        {
            base.EnterArea(opt);
            headBloodBar = Object.Instantiate(GameManager.I.headBloodBar, GameManager.I.UIRoot);
            headBloodBar.target = View.transform;
            headBloodBar.offset = headBarOffset;
            headBloodBar.text.text = $"{Property.Health}/{Property.HealthMax}";
            headBloodBar.image.fillAmount = (float)Property.Health / Property.HealthMax;

            bullet = Object.FindAnyObjectByType<SceneManager>().actorViews[2];
            firePoint = View.transform.Find("firepoint");
            rigidbody = View.GetComponent<Rigidbody>();
        }

        protected override ActorView GetViewPrefab()
        {
            return Object.FindAnyObjectByType<SceneManager>().actorViews[0];
        }

        public override void Fire(in Operation opt)
        {
            var bullet1 = Object.Instantiate(bullet, firePoint.position, firePoint.rotation);
            var rigidbody = bullet1.GetComponent<Rigidbody>();
            rigidbody.AddForce(firePoint.forward * 1000f);
            var bullet2 = bullet1.GetComponent<Bullet>();
            bullet2.target = this;
            Object.Destroy(bullet1, 5f);
        }

        public override void Resurrection(in Operation opt)
        {
            headBloodBar.gameObject.SetActive(true);
            UpdateBloodBar();
        }

        private void DeadDelay()
        {
            headBloodBar.gameObject.SetActive(false);
            GameEvent.OnPlayerDead?.Invoke();
        }

        public void OnCollisionEnter(Collision collision)
        {
            if (collision.collider.name == "PlayGround")
                isGround = true;
        }

        public void OnCollisionExit(Collision collision)
        {
            if (collision.collider.name == "PlayGround")
                isGround = false;
        }

        public override void OnUpdate()
        {
            AddOperation(new Operation(Command.SyncPosition, Identity, transform.position, transform.rotation));
        }
    }
}
#endif